package com.example.mobilesafe.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.example.mobilesafe.R;
import com.example.mobilesafe.constant.ConstantValue;
import com.example.mobilesafe.utils.SharedPreferencesUtil;
import com.example.mobilesafe.utils.ToastUtil;
import com.example.mobilesafe.view.SettingItemView;

public class SetupTwoActivity extends BaseSetupActivity {

    /**
     * SettingItemView实例
     */
    private SettingItemView siv_sim_bound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_two);

        // 初始化UI
        initUI();
    }

    @Override
    protected void showNextPage() {
        String serialNumber = SharedPreferencesUtil.getString(this, ConstantValue.SIM_NUMBER, "");
        if (!TextUtils.isEmpty(serialNumber)){
            Intent intent = new Intent(getApplicationContext(), SetupThreeActivity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.next_in_anim,R.anim.next_out_anim);
        }else {
            ToastUtil.show(this,"请绑定sim卡");
        }
    }

    @Override
    protected void showPrePage() {
        Intent intent = new Intent(getApplicationContext(), SetupOneActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.pre_in_anim,R.anim.pre_out_anim);
    }

//    /**
//     * 1.“下一页”按钮的点击方法
//     * @param view
//     */
//    public void nextPage(View view) {
//        String serialNumber = SharedPreferencesUtil.getString(this, ConstantValue.SIM_NUMBER, "");
//        if (!TextUtils.isEmpty(serialNumber)){
//            Intent intent = new Intent(getApplicationContext(), SetupThreeActivity.class);
//            startActivity(intent);
//            finish();
//            overridePendingTransition(R.anim.next_in_anim,R.anim.next_out_anim);
//        }else {
//            ToastUtil.show(this,"请绑定sim卡");
//        }
//
//    }

//    /**
//     * 2.“上一页”按钮的点击方法
//     * @param view
//     */
//    public void prePage(View view) {
//        Intent intent = new Intent(getApplicationContext(), SetupOneActivity.class);
//        startActivity(intent);
//        finish();
//        overridePendingTransition(R.anim.pre_in_anim,R.anim.pre_out_anim);
//    }

    /**
     * 3.初始化UI
     */
    private void initUI() {
        siv_sim_bound = findViewById(R.id.siv_sim_bound);
        // 0.动态获取授权
        if (ContextCompat.checkSelfPermission(SetupTwoActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(SetupTwoActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
        }
        // 1.回显（读取已有的绑定状态，用作显示，看sp中是否存储了sim卡的序列号）
        String sim_number = SharedPreferencesUtil.getString(this, ConstantValue.SIM_NUMBER, "");
        // 2.判断是否序列卡号为空
        if (TextUtils.isEmpty(sim_number)) {
            siv_sim_bound.setCheck(false);
        } else {
            siv_sim_bound.setCheck(true);
        }
        siv_sim_bound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 3.获取原有的状态
                boolean isCheck = siv_sim_bound.isCheck();
                // 4.将原有状态取反，设置给当前条目
                siv_sim_bound.setCheck(!isCheck);
                // 5.存储序列卡号
                if (!isCheck) {
                    // 6.存储
                    // 6.1 获取sim卡序列号
                    TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                    // 6.2 获取sim卡的序列卡号，注意在Android 6.0版本以上要动态申请权限
                    if (ContextCompat.checkSelfPermission(SetupTwoActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    // String simSerialNumber = manager.getSimSerialNumber(); 获取不到SIM卡号，故而写死
                    String simSerialNumber = "SIM卡号";
                    // 6.3 存储
                    SharedPreferencesUtil.putString(getApplicationContext(),ConstantValue.SIM_NUMBER,simSerialNumber);
                    Log.i("SetupTwoActivity","存储成功！" + simSerialNumber);
                }else {
                    // 7.不存储,并且删除
                    SharedPreferencesUtil.remove(getApplicationContext(),ConstantValue.SIM_NUMBER);
                    Log.i("SetupTwoActivity","存储失败！");
                }
            }
        });
    }
}